package shuanfa.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//希尔排序
public class ShellSort {
    public static void main(String[] args) {
        int[] arr=new int[]{3,4,6,7,2,7,2,8,0};
        int[] array = new int[80000];
        for (int i = 0; i < 80000; i++) {
            array[i]=(int)(Math.random()*80000);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        System.out.println("排序前时间：" + format);
        shellSort(array);
        System.out.println(Arrays.toString(arr));
        String format1 = simpleDateFormat.format(new Date());
        System.out.println("排序后的时间：" + format1);
    }

    private static void shellSort(int[] arr) {
        int preIndex ,current;//current 保存当前arr[i]数据
        for (int step = arr.length/2;step >= 1;step = step/2){//step 步长 每次减半
            for (int i = step; i < arr.length; i+=step) {
                current = arr[i];
                preIndex = i -step;
                while (preIndex >= 0 && arr[preIndex] >current){
                    arr[preIndex + step] = arr[preIndex];
                    preIndex = preIndex -step;
                }
                arr[preIndex + step] = current;
            }
        }
    }
}
